<?php
class PsychologistsController extends CController {
	public function filters() {
		return array(
				'accessControl'
		);
	}
	
	
	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}
	
	public function actionIndex() {
		$psychologists = Psychologists::model()->findAll();
		$count = count($psychologists);
		
		$this->render('psychologistsList', array(
				'psychologists' => $psychologists,
				'count' => $count
			)
		);
	}
	
	public function actionAdd() {
		$psychologist = new Psychologists;
		$qqCode = new PsychologistsMeta;
		
		if(isPost()) {
			$psychologist->attributes = $_POST['Psychologists'];
            $psychologist->userId = isset($_POST['userId']) ? (int)$_POST['userId'] : 0;
			$this->picProcess($psychologist);
			if($psychologist->save()) {
				$qqCode->psychologistId = $psychologist->psychologistId;
				$qqCode->metaKey = PsychologistsMeta::KEY_QQCODE;
				$qqCode->attributes = $_POST['PsychologistsMeta'];
				$qqCode->save();
				if(isset($_POST['direction'])) {
					$psychologistMeta = new PsychologistsMeta;
					$psychologistMeta->metaKey = PsychologistsMeta::KEY_DIRECTION;
					$psychologistMeta->psychologistId = $psychologist->psychologistId;
					$psychologistMeta->metaValue = strip_tags($_POST['direction']);
					$psychologistMeta->save();
				}
                $message = '咨询师信息添加成功。';
				Yii::app()->user->setFlash('message', $message);
				$this->redirect(array('admin/psychologists'));
			}
		}
		
		$this->render('psychologistAdd', array(
				'psychologist' => $psychologist,
				'qqCode' => $qqCode
			)
		);
	}
	
	public function actionUpdate() {
		$psychologist = $this->loadPsychologist();
		
		if(isPost()) {
			$psychologist->attributes = $_POST['Psychologists'];
			$this->picProcess($psychologist);
			$psychologist->save();
			if($psychologist->qqCode === null) {
				$psychologist->qqCode = new PsychologistsMeta;
				$psychologist->qqCode->psychologistId = $psychologist->psychologistId;
				$psychologist->qqCode->metaKey = PsychologistsMeta::KEY_QQCODE;
			}
			if(isset($_POST['direction'])) {
				$psychologistMeta = PsychologistsMeta::model()->findByMetaKey(PsychologistsMeta::KEY_DIRECTION, $psychologist);
				if($psychologistMeta === null) {
					$psychologistMeta = new PsychologistsMeta;
					$psychologistMeta->metaKey = PsychologistsMeta::KEY_DIRECTION;
					$psychologistMeta->psychologistId = $psychologist->psychologistId;
				}
				$psychologistMeta->metaValue = strip_tags($_POST['direction']);
				$psychologistMeta->save();
			}
			$psychologist->qqCode->attributes = $_POST['PsychologistsMeta'];
			$psychologist->qqCode->save();
		}
		
		$this->render('psychologistUpdate', array(
			'psychologist' => $psychologist,
			
		));
	}
	
	public function actionDelete() {
		$psychologistId = isset($_GET['id']) ? (int)$_GET['id'] : -1;
            
		if($psychologistId) {
        	$psychologist = Psychologists::model()->findByPk($psychologistId);
			if($psychologist !== null) {
				if($psychologist->delete()) {
	                PsychologistsMeta::model()->deleteAll('psychologistId = '.$psychologist->psychologistId);
                	$message = '咨询师信息删除成功。';
	                Yii::app()->user->setFlash('message', $message);
	                $this->redirect(Yii::app()->request->urlReferrer);
				}
			}
		}
	}
	
	private function picProcess(&$psychologist, $save = false, $ctrl = 'uploadedImage') {
		// Upload file now
		$uploadedPic = CUploadedFile::getInstance($psychologist, $ctrl);
		if(is_uploaded_file(isset($uploadedPic->tempName) ? $uploadedPic->tempName : '')) {
			$tmpPath = UPLOADS_TEMP.'/'.randStr();
			if($uploadedPic->saveAs($tmpPath)) {
				// Crop the image and save as file
				Yii::import('application.extensions.image.Image');
				$image = new Image($tmpPath);
				$ext = '.jpg';
				$avatar = $psychologist->psychologistId.'_'.time();
				// Make the L size thumb
				$avatarFn = $avatar.'_'.Psychologists::AVATAR_SUFFIX_L.$ext;
				$savePath = UPLOADS_PSY_AVATARS.'/'.$avatarFn;
				$flag = $image->resize(
					Psychologists::AVATAR_WIDTH_L,
					Psychologists::AVATAR_HEIGHT_L
				)->save($savePath);
				// Make the M size thumb
				$avatarFn = $avatar.'_'.Psychologists::AVATAR_SUFFIX_M.$ext;
				$savePath = UPLOADS_PSY_AVATARS.'/'.$avatarFn;
				$flag = $flag && $image->resize(
					Psychologists::AVATAR_WIDTH_M,
					Psychologists::AVATAR_HEIGHT_M
				)->save($savePath);
				$psychologist->avatar = $flag ? $avatar : $psychologist->avatar;
				if($save) {
					return $psychologist->save();
				}
			}
		}
	}
	
	public function loadPsychologist($id = null) {
		if($id !== null || isset($_GET['id'])) {
			$psychologist = Psychologists::model()->findbyPk($id !== null ? $id : (int)$_GET['id']);
		}

		if($psychologist === null) {
			throw new CHttpException(404, 'The requested psychologist does not exist.');
		}
		return $psychologist;
	}
}
